package net.jmchilton.spring.collect;

import java.util.Set;

import org.springframework.beans.factory.config.ConstructorArgumentValues;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
import org.springframework.beans.factory.xml.BeanDefinitionParser;
import org.springframework.beans.factory.xml.BeanDefinitionParserDelegate;
import org.springframework.beans.factory.xml.ParserContext;
import org.w3c.dom.Element;

public class SetTemplateParser extends TemplateBeanParser implements BeanDefinitionParser {
  public SetTemplateParser(String className, String factoryMethodName) {
    this.className = className;
    this.factoryMethodName = factoryMethodName;
  }

  @Override
  protected void parseChildren(Element element, ParserContext parserContext, AbstractBeanDefinition definition) {
    BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
    Set<?> set = delegate.parseSetElement(element, definition);
    ConstructorArgumentValues.ValueHolder valueHolder = new ConstructorArgumentValues.ValueHolder(set);
    valueHolder.setSource(parserContext.extractSource(element));
    definition.getConstructorArgumentValues().addIndexedArgumentValue(0, valueHolder);
  }
}
